﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FineUI;
using ZHDJ.BranchCenter.BLL;
using ZHDJ.Core;
using ZHDJ.Core.Util;
using ZHDJ.Data.Models.Center;
using ZHDJ.WebPage;

namespace ZHDJ.BranchCenter.Module.PartyRepresentative
{
    public partial class PartyRepresentativeInfoEdit : PageBase
    {
        #region 属性
        /// <summary>
        /// 是否和个人中心共用
        /// </summary>
        public override bool IsCommonPage
        {
            get
            {
                return true;
            }
        }

        /// <summary>
        /// 工作室ID
        /// </summary>
        public string StudioID
        {
            get
            {
                return GetQueryIntValue("StudioID"); ;
            }
        }
        #endregion
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get { return "PartyRepresentativeInfoAdd"; }
        }

        #endregion

        #region 页面属性

        public string EditorContent { get; set; }

        #endregion

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
        }

        private void BindData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
        }

        private void LoadData()
        {
            if (string.IsNullOrEmpty(StudioID))
            {
                Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                return;
            }
            string id = GetQueryIntValue("id");
            if (id != "")
            {
                var current = DB.PartyRepresentativeInfos.Find(id);
                if (current == null)
                {
                    // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }
                txtAddress.Text = current.Address;
                txtEmail.Text = current.Email;
                txtMobileNo.Text = current.MobileNo;
                txtOrderNo.Text = current.OrderNo.ToString();
                txtPost.Text = current.Post;
                ddlCategory.SelectedValue = current.Category;
                ddlCategory.Enabled = false;
                ppMember.Enabled = false;
                txtUserName.Enabled = false;
                frUserName.Hidden = true;
                frppMember.Hidden = true;
                if (ddlCategory.SelectedValue == "1")
                {
                    ppMember.Text = ControlBusiness.GetMembersPickerText(current.LoginID);
                    ppMember.Value = current.LoginID;
                    frppMember.Hidden = false;
                }
                else
                {
                    txtUserName.Text = current.UserName;
                    frUserName.Hidden = false;
                }
                hdFileId.Text = current.ImageID;
                if (!string.IsNullOrEmpty(hdFileId.Text))
                {
                    imgPhoto.ImageUrl = UpFileBusiness.GetFileUrl(hdFileId.Text);
                    imgPhoto.ImageWidth = Unit.Pixel(300);

                    PageContext.RegisterStartupScript("ReDoLayout();");
                }
            }
        }

        #endregion

        #region Events

        private void SaveData()
        {
            if (string.IsNullOrEmpty(StudioID))
            {
                Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                return;
            }
            string id = GetQueryIntValue("id");
            string imageUrl = string.Empty;
            PartyRepresentativeInfo editModel;
            if (id != "")
            {
                //更新
                editModel = DB.PartyRepresentativeInfos.Find(id);
                if (editModel == null)
                {
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }
                editModel.ModifyTime = DateTime.Now;
                editModel.ModifyBy = CurrentUser.LoginID;
            }
            else
            {
                editModel = new PartyRepresentativeInfo();
                editModel.ID = StringHelper.NewID;
                editModel.CreateTime = DateTime.Now;
                editModel.ModifyTime = DateTime.Now;
                editModel.CreateBy = CurrentUser.LoginID;
                editModel.ModifyBy = CurrentUser.LoginID;

                DB.PartyRepresentativeInfos.Add(editModel);
            }
            editModel.StudioID = StudioID;
            editModel.OrderNo = Convert.ToInt32(txtOrderNo.Text);
            editModel.Category = ddlCategory.SelectedValue;
            if (!string.IsNullOrEmpty(hdFileId.Text))
            {
                editModel.ImageID = hdFileId.Text;
            }
            editModel.Address = txtAddress.Text;
            editModel.Email = txtEmail.Text;
            editModel.MobileNo = txtMobileNo.Text;
            editModel.Post = txtPost.Text;
            editModel.Category = ddlCategory.SelectedValue;
            if (editModel.Category == "1")
            {
                editModel.LoginID = ppMember.Value;
                editModel.UserName = ppMember.Text;
            }
            else
            {
                editModel.LoginID = "";
                editModel.UserName = txtUserName.Text;
            }
            DB.SaveChanges();
        }

        /// <summary>
        /// 保存并关闭按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveData();

            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }


        /// <summary>
        /// 上传
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void filePhoto_FileSelected(object sender, EventArgs e)
        {
            var rs = UpFileBusiness.Upload(filePhoto, CurrentUser.LoginID, "党代表头像", SysEnum.UploadFileType.uploadimage);
            if (rs.IsSuccess)
            {
                hdFileId.Text = rs.FileID;
                imgPhoto.ImageUrl = "~/" + rs.FileUrl;
                imgPhoto.ImageWidth = Unit.Pixel(300);

                PageContext.RegisterStartupScript("ReDoLayout();");
            }
            else
            {
                Alert.Show("上传失败！" + rs.Message, String.Empty);
            }
        }



        #endregion

        protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            frUserName.Hidden = true;
            frppMember.Hidden = true;
            if (ddlCategory.SelectedValue == "1")
            {
                frppMember.Hidden = false;
            }
            else
            {
                frUserName.Hidden = false;
            }
        }
    }
}